Imports System.Data
Imports BlazeApps.Library.Form
Imports AjaxControlToolkit

Partial Class Usercontrols_Form_FormViewer
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Protected Property FormGuid() As Guid
        Get
            Return GetGuidFromVS("FormID")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("FormID", value)
        End Set
    End Property

    Protected Property FormResponseGuid() As Guid
        Get
            Return GetGuidFromVS("FormResponseID")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("FormResponseID", value)
        End Set
    End Property

    Protected Property FormQuestionGuid() As Guid
        Get
            Return GetGuidFromVS("FormQuestionID")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("FormQuestionID", value)
        End Set
    End Property

    Protected Property FormEditMode() As Boolean
        Get
            Return GetBooleanFromVS("FormEditMode")
        End Get
        Set(ByVal value As Boolean)
            ViewState.Add("FormEditMode", value)
        End Set
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.MessageLabel = Me.uxMessagesLabel
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        SetPageTitle("Form Viewer")

		If IsUserAuthenticated And (IsEditor() OrElse IsReviewer()) Then

			GetQueryStringValues()
			BuildPage(FormGuid, FormResponseGuid, Me.uxControlsPlaceHolder, "formsystem", True, False)

			If Page.IsPostBack = False Then
				LoadResponse()
			End If

		Else
			Me.uxFromViewerPanel.Visible = False
			Me.uxMessagesLabel.Text = ShowControls(SecurityMask.Reviewer, True)
		End If

    End Sub

#End Region

#Region " Save Data "

    Private Sub SaveReviewed()
        Dim item As FormResponse = FormResponse.GetFormResponse(FormResponseGuid)

		item.Reviewed = uxReviewedCheckBox.Checked
        item.ReviewedBy = CurrentUserID()
		item.ReviewedDateString = DateTime.Now

		If Not (String.IsNullOrEmpty(Me.uxFormCommentsTextBox.Text)) Then
			item.ReviewerComments = Me.uxFormCommentsTextBox.Text.Trim()
		End If

        item.Save()
    End Sub

#End Region

#Region " Load Data "

    Private Sub LoadResponse()
        Dim item As FormResponse = FormResponse.GetFormResponse(FormResponseGuid)

        Me.uxFormCommentsTextBox.Text = item.ReviewerComments

    End Sub

#End Region

#Region " Form Controls Events "

	Protected Sub uxSaveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxSaveButton.Click
		SaveReviewed()
	End Sub

#End Region

#Region " Helpers and Methods "

    Protected Sub GetQueryStringValues()
        FormGuid = GetGuidFromQueryString("FormID")
        FormResponseGuid = GetGuidFromQueryString("FormResponseID")
    End Sub

#End Region

End Class
